﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; 
using System.Text; 
using System.Windows.Forms;
using  mshtml;
namespace Facebook_Chat_BOT
{
    public partial class Form1 : Form
    {
        bool sendMsg = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("https://m.facebook.com/messages");
        }
        public void NavigateToNewUser()
        {
            try
            {
                webBrowser1.Url = new Uri("https://m.facebook.com/messages");
                label1.Text = "Checking New Messages";
                String objectsconti = "objects_container";

                // MessageBox.Show("");
                String stt = webBrowser1.DocumentText;
                
                int firstaccesskey =webBrowser1.DocumentText.IndexOf("A accessKey=5");
                int objecttcontainerrr = stt.IndexOf(objectsconti, firstaccesskey);
                String hrefText = stt.Substring(firstaccesskey, objecttcontainerrr - firstaccesskey);
                int message1Occourance = hrefText.LastIndexOf("messages");
                int endOflink = hrefText.IndexOf(">", message1Occourance);

                String UserURL = hrefText.Substring(message1Occourance, endOflink - message1Occourance - 1);

                webBrowser1.Url = new Uri("https://m.facebook.com/" + UserURL);
                sendMsg = true;

                //Send Message

            }
            catch (Exception ex)
            {

                label1.Text = "No New Message from any User";

            }

        }
        private void button3_Click(object sender, EventArgs e)
        {
            NavigateToNewUser();
        }
        public void SendMessage()
        {

            if (sendMsg == true)
            {
                webBrowser1.Focus();
                HtmlElement headElement = webBrowser1.Document.GetElementById("composerInput");//[0];//.GetElementById("composerInput");//.GetElementsByTagName("head")[0];
                headElement.InnerText = textBox1.Text;
                HtmlElement headElement1 = webBrowser1.Document.GetElementById("send");
                headElement1.InvokeMember("click");
                sendMsg = false;
                System.Threading.Thread.Sleep(3000);
                webBrowser1.Url = new Uri("https://m.facebook.com/messages");
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            SendMessage();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            NavigateToNewUser();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.facebook.com/IndianServersPage");
        }
    }
}
